package com.company.test27_28;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by Administrator on 2016.11.8.
 */
public class KnightTourModel {
    private UnweightedGraph<Integer> graph;

    public KnightTourModel(){
        graph=new UnweightedGraph<Integer>(getEdges(),64);
    }

    private List<AbstractGraph.Edge> getEdges(){
        List<AbstractGraph.Edge> edges=new ArrayList<>();
        for(int i=0;i<64;i++){
            addEdges(edges,i);
        }
        return edges;
    }

    private void addEdges(List<AbstractGraph.Edge> edges,int sign){
        int x=sign%8;
        int y=sign/8;
        int[][] change={
                {1,2},{1,-2},{-1,2},{-1,-2},{2,1},{2,-1},{-2,1},{-2,-1}
        };

        for(int i=0;i<change.length;i++){
            AbstractGraph.Edge edge=edge(x+change[i][0],y+change[i][1],sign);
            if(edge!=null){
                edges.add(edge);
            }
        }
    }
    private static AbstractGraph.Edge edge(int x,int y,int begin){
        if(x>=0&&x<7&&y>=0&&y<7){
            return new AbstractGraph.Edge(begin,y*8+x);
        }
        return null;
    }

    public static void main(String[] args) {
        KnightTourModel king=new KnightTourModel();
        List list=king.graph.getHamiltonianPath(24);
        for(int i=0;i<list.size();i++){
            System.out.print(list.get(i)+" ");
            if(i%10==9)
                System.out.println();
        }
    }
}